package com.cn.kehong.controller;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.cn.kehong.api.*;
import com.cn.kehong.domain.*;
import com.cn.kehong.utils.CommonUtil;
import com.cn.kehong.utils.JsonResponse;
import com.cn.kehong.utils.QueryForm;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.net.URLDecoder;
import java.util.List;

@Controller
@RequestMapping("/exceptionManagement")
public class ExceptionManagementController {

    @Autowired
    private IExceptionManagementService exceptionManagementService;

    @RequestMapping("/getExceptionManagementList/{frontToken}")
    @ResponseBody
    public JsonResponse<List<ExceptionManagement>> getExceptionManagementList(@PathVariable(value = "frontToken", required = true) String frontToken, QueryForm queryForm) throws Exception {
        JsonResponse<List<ExceptionManagement>> response = new JsonResponse<>();
        List<ExceptionManagement> productPackageVos = exceptionManagementService.getExceptionManagementList(queryForm);
        response.setResult(productPackageVos);
        response.setSuccess(true);
        return response;
    }

    //取消接口
    @RequestMapping("/exceptionManagementCancle/{frontToken}")
    @ResponseBody
    public JsonResponse<String> exceptionManagementCancle(String id, @PathVariable(value = "frontToken",required = true) String frontToken) throws Exception{
        JsonResponse<String> response = new JsonResponse<>();
        String result = exceptionManagementService.exceptionManagementCancle(id);
        return CommonUtil.getResponseData(response,result);
    }


    //暂存
    @RequestMapping("/temporarily/{frontToken}")
    @ResponseBody
    public JsonResponse<String> temporarily(@RequestBody ExceptionManagement exceptionManagement, @PathVariable(value =  "frontToken",required = true) String frontToken) throws Exception{
        JsonResponse<String> response = new JsonResponse<>();
        String result = exceptionManagementService.temporarily(exceptionManagement);
        return CommonUtil.getResponseData(response,result);
    }

    //新增
    @RequestMapping("/addExceptionManagement/{frontToken}")
    @ResponseBody
    public JsonResponse<String> addExceptionManagement(@RequestBody ExceptionManagement exceptionManagement, @PathVariable(value =  "frontToken",required = true) String frontToken) throws Exception{
        JsonResponse<String> response = new JsonResponse<>();
        String result = exceptionManagementService.addExceptionManagement(exceptionManagement);
        return CommonUtil.getResponseData(response,result);
    }

    // 修改
    @RequestMapping("/updateExceptionManagement/{frontToken}")
    @ResponseBody
    public JsonResponse<String> updateExceptionManagement(@RequestBody ExceptionManagement exceptionManagement, @PathVariable(value =  "frontToken",required = true) String frontToken) throws Exception {
        JsonResponse<String> response = new JsonResponse<>();
        String result = exceptionManagementService.updateExceptionManagement(exceptionManagement);
        return CommonUtil.getResponseData(response, result);
    }


    // 已处置Excel下载
    @ResponseBody
    @RequestMapping("/exportExceptionManagement")
    public void exportExceptionManagement(String queryStr, HttpServletResponse response) throws Exception {
        QueryForm queryForm = new QueryForm();
        if(StringUtils.isNotEmpty(queryStr)){
            queryStr = URLDecoder.decode(queryStr,"utf-8");
            queryStr = new String(queryStr.getBytes("ISO-8859-1"),"utf-8");
            JSONObject jsonObject = JSON.parseObject(queryStr);
            queryForm.setFactoryName(jsonObject.getString("factoryName"));
            queryForm.setSectionProcess(jsonObject.getString("sectionProcess"));
            queryForm.setAbnormalType(jsonObject.getString("abnormalType"));
            queryForm.setProductCode(jsonObject.getString("productCode"));
            queryForm.setAbnormalLevel(jsonObject.getString("abnormalLevel"));
            queryForm.setHandlingMethod(jsonObject.getString("handlingMethod"));
            queryForm.setStartTime(jsonObject.getString("startTime"));
            queryForm.setEndTime(jsonObject.getString("endTime"));
        }
        exceptionManagementService.exportExceptionManagement(queryForm, response);
    }


}
